package com.example.myprojectoffeixue;
import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.localization.Gateway;
import com.localization.SendUtil;
import com.localization.SocketUdpClient;
import org.apache.http.entity.StringEntity;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.jpush.android.api.JPushInterface;
public class MyApplication extends Application {
    public static boolean localflag=false;
    public static SocketUdpClient udp;
    public  HouseUtil Nodevice_house;
    public HouseUtil hu;//当前的房间
    public ArrayList<HouseUtil> all_House=new ArrayList<HouseUtil>();
    public String name_house;//房间的名字
    public ArrayList<GateWay> m_gateway=new ArrayList<>();
    String name;
    //重连子线程
    Thread reconnect = new Thread() {
        @Override
        public void run() {

            while (c.getReadyState()!=1) {
                try {
                    sleep(5000);
                    flag = 1;
                    if (MyApplication.flag8!=1&&c.getReadyState()!=1) {
                        Log.d("触发", "重连");
                        hand1.sendEmptyMessage(15);
                    }
                } catch (InterruptedException e) {
                    flag = 0;
                    e.printStackTrace();


                }
            }
            flag = 0;
        }


    };
    //自定义application主要功能是websocket连接服务器,以及发送命令

    //重连消息
    Handler hand1 = new Handler() {

        public void handleMessage(android.os.Message msg) {

            if (msg.what == 15) {
                connect();
            }
        }
    };
    //设备网关上下线，踢下线逻辑
    Handler hand2 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 26) {
                String devicId = (String) msg.obj;
                String s_msg = null;
                if ("0100".equals(devicId)) {
                    s_msg = "存在开关掉线";
                } else if ("010B".equals(devicId) || "010A".equals(devicId)||"0110".equals(devicId)) {
                    s_msg = "存在门锁掉线";
                } else if ("010D".equals(devicId)) {
                    s_msg = "存在门磁掉线";
                } else if ("010E".equals(devicId)) {
                    s_msg = "存在红外掉线";
                } else if ("010C".equals(devicId)) {
                    s_msg = "存在取电开关掉线";
                } else if ("0002".equals(devicId)) {
                    s_msg = "存在窗帘掉线";
                } else if ("010F".equals(devicId)) {
                    s_msg = "存在中央空调掉线";
                } else if ("0020".equals(devicId)) {
                    s_msg = "存在红外转发器掉线";
                } else if ("0009".equals(devicId)) {
                    s_msg = "存在智能插座掉线";
                } else if ("gatway_close".equals(devicId)) {
                    s_msg = "存在网关掉线";
                } else if ("gateway_open".equals(devicId)) {
                    s_msg = "存在网关上线";
                }
                ToastUtils.showToast(getBaseContext(), s_msg);
            } else if (msg.what == 25) {
                String devicId = (String) msg.obj;
                String s_msg = null;

                if ("0100".equals(devicId)) {
                    s_msg = "存在开关上线";
                } else if ("010B".equals(devicId) || "010A".equals(devicId)||"0110".equals(devicId)) {
                    s_msg = "存在门锁上线";
                } else if ("010D".equals(devicId)) {
                    s_msg = "存在门磁上线";
                } else if ("010E".equals(devicId)) {
                    s_msg = "存在红外上线";
                } else if ("010C".equals(devicId)) {
                    s_msg = "存在取电开关上线";
                } else if ("0002".equals(devicId)) {
                    s_msg = "存在窗帘上线";
                } else if ("010F".equals(devicId)) {
                    s_msg = "存在中央空调上线";
                } else if ("0020".equals(devicId)) {
                    s_msg = "存在红外转发器上线";
                } else if ("0009".equals(devicId)) {
                    s_msg = "存在智能插座上线";
                } else if ("gateway_close".equals(devicId)) {
                    s_msg = "存在网关掉线";
                }
                ToastUtils.showToast(getBaseContext(), s_msg);
            } else if (msg.what == 27) {
                Intent intent = new Intent();
                ComponentName cm = new ComponentName("com.example.myprojectoffeixue", "com.example.myprojectoffeixue.Dialog_all");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(cm);
                startActivity(intent);
            }
        }
    };
    public static Handler hand = new Handler();
    public int ScenId = -1;
    public CJutil cj;
    public int time_value;
    public String ieee_ykq;
    public String port_ykq;
    public String mac_ykq;
    public String mac_user;
    public String SSID;
    public String Host_SSid;
    public String Host_Password;
    public String Host_jami;
    public SheBeiClass sb;
    public List<SheBeiClass> list;
    public String type;
    public boolean flag6;
    public static HashMap<Integer, String> YuanSM = new HashMap<>();
    public static int AnNiuId = -1;
    public static ArrayList<String> MK = new ArrayList<String>();
    public Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    //自己的总设备
    public ArrayList<SheBeiClass> lsb9 = new ArrayList<SheBeiClass>();
    //所有的设备
    public ArrayList<SheBeiClass> lsb10 = new ArrayList<SheBeiClass>();
    //开关
    public static ArrayList<SheBeiClass> lsb = new ArrayList<SheBeiClass>();
    //门锁
    public static ArrayList<SheBeiClass> lsb1 = new ArrayList<SheBeiClass>();
    //红外
    public  static ArrayList<SheBeiClass> lsb2 = new ArrayList<SheBeiClass>();
    //窗帘
    public static ArrayList<SheBeiClass> lsb3 = new ArrayList<SheBeiClass>();
    //门磁
    public static ArrayList<SheBeiClass> lsb4 = new ArrayList<SheBeiClass>();
    //取电开关
    public static ArrayList<SheBeiClass> lsb5 = new ArrayList<SheBeiClass>();
    //遥控器
    public static ArrayList<SheBeiClass> lsb6 = new ArrayList<SheBeiClass>();
    //中央空调
    public static ArrayList<SheBeiClass> lsb7 = new ArrayList<SheBeiClass>();
    //智能插座
    public static ArrayList<SheBeiClass> lsb8 = new ArrayList<SheBeiClass>();
    public ArrayList<Extra> scen = new ArrayList<Extra>();
    public ArrayList<CJutil> s_cj = new ArrayList<CJutil>();
    public ArrayList<CJutil> l_cj = new ArrayList<CJutil>();
    public ArrayList<CJutil> time_cj = new ArrayList<CJutil>();
    String j;
    public String userId;
    public String username;
    public String authId;
    public String authToken;
    public String mac;
    public ArrayList<Websocketutil> l;
    PackageInfo pi;
    //空调的额外信息
    public static  Extra t;
    public static HashMap<String, Extra> kt_map = new HashMap<String, Extra>();
    // 开放平台申请的APP key & secret key
    public MyWebSocketClient c;
    public boolean flag3 = false;//判断是否连接成功的状态位
    public boolean flag7 = true;//是否重连的状态位
    public int flag5 = 0;//开启子线程还是继续子线程的状态位
    public int flag = 0;//是否重连的状态位
    public static int flag8 = 0;//判断是否触发关闭重连的状态位
    public Map<String, ArrayList<SheBeiClass>> list_sb;
    // public WebSocketImpl wsc;
    public MyApplication() {
        // TODO Auto-generated constructor stub
    }
    //
    @Override
    public void onCreate() {
        SendUtil.SendThread.start();
        Log.d("creat","app启动");
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        CrashHandler catchHandler = CrashHandler.getInstance();
        catchHandler.init(getApplicationContext());
        flag6 = true;
        super.onCreate();
    }
    public void connect() {
        if (c != null) {
            Log.d("jjjjj", "lllll"+"--"+c.getReadyState()+c);
            c.close();
        }
        Log.d("我重连了", "重连了");
        String wsUrl = "ws://" + UserClass.WSServerIP + ":9991/connect?authToken=" + authToken + "&authId=" + authId;
       // String wsUrl = "ws://" + UserClass.WSServerIP + ":11342/connect?authToken=" + authToken + "&authId=" + authId;
        Log.d("wsUrl", wsUrl);
        try {
            // 开启子线程每90秒发送一次心跳包

            // TODO Auto-generated catch block
            c = new MyWebSocketClient(new URI(wsUrl), new Draft_17()) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                    Log.d("连接open打开",""+c);
                    flag3 = true;
                    Get();
                    SheBeiClass sb = new SheBeiClass("1000");
                    j = JSON.toJSONString(sb);
                    // 开启子线程每90秒发送一次心跳包
                    new Thread() {
                        public void run() {
                            while (true) {
                                if (c.getReadyState()==1) {
                                    Log.d("心跳包", c + "" + flag3);

                                    if (c != null) {
                                        c.send(j);
                                    }
                                } else {
                                    return;
                                }
                                try {
                                    sleep(90000);
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                    }.start();
                }

                @Override
                public void onMessage(String s) {
                    s = AESUtil.decrypt(s);
                    Message msg2 = new Message();
                    msg2.what = 50;
                    msg2.obj = s;
                    hand2.sendMessage(msg2);
                    Log.d("sssssssssssssssssssssss", s);
                    JSONObject jb = JSONObject.parseObject(s);
                    String cmd = jb.getString("cmd");
                    String ie1 = jb.getString("ieee");
                    String pt1= jb.getString("port");
                    String st1 = jb.getString("status");
                    String mac=jb.getString("mac");
                  if(Gateway.MacSocke.containsKey(mac)){
                            Log.d("不解析","云端不解析");
                            return;
                        }
                    for(int x=0;x<all_House.size();x++){

                        for(int y=0;y<all_House.get(x).devices.size();y++) {
                            if (ie1.equals(all_House.get(x).devices.get(y).ieee) && (pt1.equals(all_House.get(x).devices.get(y).port))) {
                                all_House.get(x).devices.get(y).status = st1;
                                if (hand != null) {
                                    Message msg = new Message();
                                    msg.what = 111;
                                    hand.sendMessage(msg);
                                    //操作设备成功反馈
                                }
                            }
                        }


                    }
                    if ("2007".equals(cmd)) {
                        String ie = jb.getString("ieee");
                        String pt = jb.getString("port");
                        String st = jb.getString("status");
                        for (int i = 0; i < lsb.size(); i++) {
                            if (ie.equals(lsb.get(i).ieee) && (pt.equals(lsb.get(i).port))) {
                                lsb.get(i).status = st;
                                if (hand != null) {
                                    Log.d("云端解析","云端");
                                    Message msg = new Message();
                                    msg.what = 0;
                                    msg.obj = "1007";
                                    hand.sendMessage(msg);
                                    //操作设备成功反馈
                                }
                            }
                        }
                        for (int i = 0; i < lsb8.size(); i++) {

                            if (ie.equals(lsb8.get(i).ieee) && (pt.equals(lsb8.get(i).port))) {
                                lsb8.get(i).status = st;
                                if (hand != null) {
                                    Message msg = new Message();
                                    msg.what = 0;
                                    msg.obj = "1007";
                                    hand.sendMessage(msg);
                                }
                            }
                        }

                    } else if ("2005".equals(cmd)) {

                        String ie = jb.getString("ieee");
                        String pt = jb.getString("port");
                        String st = jb.getString("status");

                        for (int i = 0; i < lsb1.size(); i++) {
                            if (ie.equals(lsb1.get(i).ieee) && (pt.equals(lsb1.get(i).port))) {
                                lsb1.get(i).status = st;
                                if (hand != null) {
                                    Message msg = new Message();
                                    msg.what = 0;
                                    msg.obj = "1005";
                                    hand.sendMessage(msg);
                                }
                            }
                        }
                    } else if ("2009".equals(cmd)) {
                        String ie = jb.getString("ieee");
                        String pt = jb.getString("port");
                        String st = jb.getString("status");
                        String time = jb.getString("time");
                        Log.d("menci", "menci");
                        for (int i = 0; i < lsb4.size(); i++) {
                            if (ie.equals(lsb4.get(i).ieee) && (pt.equals(lsb4.get(i).port))) {
                                lsb4.get(i).status = st;
                                if (hand != null) {
                                    Message msg = new Message();
                                    msg.what = 0;
                                    msg.obj = "1009";
                                    hand.sendMessage(msg);
                                }
                            }
                        }
                    } else if ("2010".equals(cmd)) {
                        String ie = jb.getString("ieee");
                        String pt = jb.getString("port");
                        String st = jb.getString("status");
                        String stime = jb.getString("time");
                        for (int i = 0; i < lsb2.size(); i++) {
                            if (ie.equals(lsb2.get(i).ieee) && (pt.equals(lsb2.get(i).port))) {
                                lsb2.get(i).status = st;
                                if (hand != null) {
                                    if (!"04".equals(st)) {
                                        Message msg = new Message();
                                        msg.what = 0;
                                        msg.obj = "1010";
                                        hand.sendMessage(msg);
                                    } else {
                                        hand.sendEmptyMessage(60);
                                    }
                                }
                            }
                        }
                    } else if ("2006".equals(cmd)) {
                        String ie = jb.getString("ieee");
                        String pt = jb.getString("port");
                        String st = jb.getString("status");
                        if ("06".equals(st)) {
                            String power = jb.getString("power");
                            Message msg = new Message();
                            msg.obj = power;
                            msg.what = 3;//获取取电开关电量成功反馈
                            if (hand != null) {
                                hand.sendMessage(msg);
                            }
                        }
                        {
                            for (int i = 0; i < lsb5.size(); i++) {
                                if (ie.equals(lsb5.get(i).ieee) && (pt.equals(lsb5.get(i).port))) {
                                    lsb5.get(i).status = st;
                                    if (hand != null) {
                                        Message msg = new Message();
                                        msg.what = 0;
                                        msg.obj = "1006";
                                        hand.sendMessage(msg);
                                    }
                                }
                            }
                        }

                    } else if ("2011".equals(cmd)) {
                        String status = jb.getString("status");
                        if ("06".equals(status)) {
                            System.out.println(j);
                            String ieee = jb.getString("ieee");
                            String port = jb.getString("port");
                            String Key = ieee + port;
                            JSONObject n = jb.getJSONObject("extra");
                            t = JSON.parseObject(n.toString(), Extra.class);
                            kt_map.put(Key, t);
                            if (hand != null) {
                                hand.sendEmptyMessage(32);
                            }
                        }

                    } else if ("2012".equals(cmd)) {

                        String status = jb.getString("status");

                        if ("03".equals(status)) {
                            JSONObject j1 = jb.getJSONObject("extra");
                            JSONArray p = j1.getJSONArray("codeIndexList");
                            MK.clear();
                            for (Object o : p) {
                                MK.add((String) o);
                            }

                            if (hand != null) {
                                hand.sendEmptyMessage(1);
                                //获取码库列表成功反馈
                            }

                        } else if ("02".equals(status)) {
                            if (AnNiuId != -1) {
                                YuanSM.put(AnNiuId, jb.getString("extra"));
                                if (hand != null) {
                                    hand.sendEmptyMessage(2);
                                    //获取按键源码成功反馈
                                }
                            }
                        }

                    } else if ("2200".equals(cmd)) {
                        //新设备入网成功反馈
                        String deviceId = null;
                        JSONArray jar = jb.getJSONArray("devices");
                        ArrayList<SheBeiClass> sb_SX = (ArrayList<SheBeiClass>) JSONArray.parseArray(jar.toString(), SheBeiClass.class);
                        for (int i = 0; i < sb_SX.size(); i++) {
                            deviceId = sb_SX.get(0).deviceId;
                        }
                        if (hand2 != null) {
                            Message msg = new Message();
                            msg.what = 25;
                            msg.obj = deviceId;
                            GetDeviceList();
                            hand2.sendMessage(msg);
                        }
                        if (hand != null) {
                            Message msg = new Message();
                            msg.what = 25;
                            msg.obj = deviceId;
                            hand.sendMessage(msg);
                        }

                    } else if ("2201".equals(cmd)) {
                        Log.d("设备掉线", "diaoxian");
                        String deviceId = null;
                        JSONArray jar = jb.getJSONArray("devices");
                        ArrayList<SheBeiClass> sb_DX = (ArrayList<SheBeiClass>) JSONArray.parseArray(jar.toString(), SheBeiClass.class);
                        for (int i = 0; i < sb_DX.size(); i++) {
                            deviceId = sb_DX.get(0).deviceId;
                        }

                        if (hand2 != null) {
                            Message msg = new Message();
                            msg.what = 26;
                            msg.obj = deviceId;
                            GetDeviceList();
                            hand2.sendMessage(msg);
                        }

                    } else if ("2202".equals(cmd)) {
                        Log.d("网关上线", "shangxian");
                        if (hand2 != null) {
                            Message msg = new Message();
                            msg.what = 26;
                            msg.obj = "gateway_open";
                            GetDeviceList();
                            hand2.sendMessage(msg);

                        }
                        // ToastUtils.showToast(getApplicationContext(),"存在网关上线");

                    } else if ("2203".equals(cmd)) {
                        Log.d("网管电线", "diaoxain");
                        if (hand2 != null) {
                            Message msg = new Message();
                            msg.what = 26;
                            msg.obj = "gatway_close";
                            GetDeviceList();
                            hand2.sendMessage(msg);
                        }
                        // ToastUtils.showToast(getApplicationContext(),"存在网关掉线");
                    } else if ("2206".equals(cmd)) {

                        Log.d("踢下线", "heheh"+c);
                        flag8 = 1;
                        flag3 = false;
                        Message msg = new Message();
                        msg.what = 27;
                        hand2.sendMessage(msg);
                    }
                }

                @Override
                public void onClose(int i, String s, boolean b) {
                    Log.d("关闭触发", "hhhhhh" + flag8+c.getReadyState()+c);
                    flag3 = false;
                    if(flag8==0){
                        if (flag == 0) {
                            if (flag5 == 0) {
                                reconnect.start();
                                flag5 = 1;
                            } else {
                                reconnect.run();
                            }
                        }
                }
                }
                @Override
                public void onError(Exception e) {
                    Log.d("错误关闭", "888888888888888" + e.toString()+c.getReadyState()+"--"+c);
                    if(c!=null){
                        c.close();
                    }
                }
            };
            c.connect();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public  void Get() {
        lsb9.clear();
        lsb.clear();
        lsb1.clear();
        lsb2.clear();
        lsb3.clear();
        lsb4.clear();
        lsb5.clear();
        lsb6.clear();
        lsb7.clear();
        lsb8.clear();
        for (int i = 0; i < m_gateway.size(); i++) {
            for (int j = 0; j < m_gateway.get(i).devices.size(); j++) {
                if (m_gateway.get(i).mac != null) {
                    m_gateway.get(i).devices.get(j).mac = m_gateway.get(i).mac;
                }
                if ("0100".equals(m_gateway.get(i).devices.get(j).deviceId)) {
                    // 开关
                    if (m_gateway.get(i).devices.get(j).deviceName == null) {
                        m_gateway.get(i).devices.get(j).deviceName = "智能开关";
                    }

                    if (lsb.size() == 0) {
                        lsb.add(m_gateway.get(i).devices.get(j));
                    } else {
                        int h = 0;
                        for (int g = 0; g < lsb.size(); g++) {
                            if (m_gateway.get(i).devices.get(j).ieee.equals(lsb.get(g).ieee) && m_gateway.get(i).devices.get(j).port.equals(lsb.get(g).port)) {
                                h = 1;
                            }
                        }

                        if (h == 0) {
                            lsb.add(m_gateway.get(i).devices.get(j));
                        }
                    }

                    if ("1".equals(m_gateway.get(i).type)) {
                        lsb9.add(m_gateway.get(i).devices.get(j));
                    }
                } else if ("010B".equals(m_gateway.get(i).devices.get(j).deviceId) || "010A".equals(m_gateway.get(i).devices.get(j).deviceId)||"0110".equals(m_gateway.get(i).devices.get(j).deviceId)) {
                    // 门锁
                    if (m_gateway.get(i).devices.get(j).deviceName == null) {
                        m_gateway.get(i).devices.get(j).deviceName = "智能门锁";
                    }
                    if (m_gateway.get(i).devices.get(j).status == null) {
                        m_gateway.get(i).devices.get(j).status = "00";
                    }
                    //  lsb1.add(m_gateway.get(i).devices.get(j));


                    if (lsb1.size() == 0) {
                        lsb1.add(m_gateway.get(i).devices.get(j));
                    } else {
                        int h = 0;
                        for (int g = 0; g < lsb1.size(); g++) {
                            if (m_gateway.get(i).devices.get(j).ieee.equals(lsb1.get(g).ieee) && m_gateway.get(i).devices.get(j).port.equals(lsb1.get(g).port)) {

                                h = 1;
                            }



                        }

                        if (h == 0) {
                            lsb1.add(m_gateway.get(i).devices.get(j));
                        }
                    }


                    if ("1".equals(m_gateway.get(i).type)) {
                        lsb9.add(m_gateway.get(i).devices.get(j));
                    }
                } else if ("010D".equals(m_gateway.get(i).devices.get(j).deviceId)) {
                    // 门磁
                    if (m_gateway.get(i).devices.get(j).deviceName == null) {
                        m_gateway.get(i).devices.get(j).deviceName = "智能门磁";
                    }
                    if (lsb4.size() == 0) {


                        lsb4.add(m_gateway.get(i).devices.get(j));
                    } else {
                        int h = 0;
                        for (int g = 0; g < lsb4.size(); g++) {
                            if (m_gateway.get(i).devices.get(j).ieee.equals(lsb4.get(g).ieee) && m_gateway.get(i).devices.get(j).port.equals(lsb4.get(g).port)) {
                                h = 1;
                            }
                        }

                        if (h == 0) {
                            lsb4.add(m_gateway.get(i).devices.get(j));
                        }

                    }

                    if ("1".equals(m_gateway.get(i).type)) {
                        lsb9.add(m_gateway.get(i).devices.get(j));
                    }


                } else if ("010E".equals(m_gateway.get(i).devices.get(j).deviceId)) {
                    // 红外
                    if (m_gateway.get(i).devices.get(j).deviceName == null) {
                        m_gateway.get(i).devices.get(j).deviceName = "红外人体感应";
                    }
                    if (lsb2.size() == 0) {
                        lsb2.add(m_gateway.get(i).devices.get(j));
                    } else {
                        int h = 0;
                        for (int g = 0; g < lsb2.size(); g++) {
                            if (m_gateway.get(i).devices.get(j).ieee.equals(lsb2.get(g).ieee) && m_gateway.get(i).devices.get(j).port.equals(lsb2.get(g).port)) {
                                h = 1;
                            }
                        }

                        if (h == 0) {


                            lsb2.add(m_gateway.get(i).devices.get(j));


                        }


                    }


                    if ("1".equals(m_gateway.get(i).type)) {
                        lsb9.add(m_gateway.get(i).devices.get(j));
                    }


                } else if ("010C".equals(m_gateway.get(i).devices.get(j).deviceId)) {
                    // 取电开关
                    if (m_gateway.get(i).devices.get(j).deviceName == null) {
                        m_gateway.get(i).devices.get(j).deviceName = "取电开关";
                    }
                    if (lsb5.size() == 0) {
                        lsb5.add(m_gateway.get(i).devices.get(j));
                    } else {
                        int h = 0;
                        for (int g = 0; g < lsb5.size(); g++) {
                            if (m_gateway.get(i).devices.get(j).ieee.equals(lsb5.get(g).ieee) && m_gateway.get(i).devices.get(j).port.equals(lsb5.get(g).port)) {

                                h = 1;

                            }


                        }

                        if (h == 0) {
                            lsb5.add(m_gateway.get(i).devices.get(j));

                        }


                    }


                    if ("1".equals(m_gateway.get(i).type)) {
                        lsb9.add(m_gateway.get(i).devices.get(j));
                    }


                } else if ("0002".equals(m_gateway.get(i).devices.get(j).deviceId)) {

                    // 窗帘
                    if (m_gateway.get(i).devices.get(j).deviceName == null) {
                        m_gateway.get(i).devices.get(j).deviceName = "智能窗帘";
                    }
                    if (lsb3.size() == 0) {
                        lsb3.add(m_gateway.get(i).devices.get(j));
                    } else {
                        int h = 0;
                        for (int g = 0; g < lsb3.size(); g++) {
                            if (m_gateway.get(i).devices.get(j).ieee.equals(lsb3.get(g).ieee) && m_gateway.get(i).devices.get(j).port.equals(lsb3.get(g).port)) {

                                h = 1;
                            }


                        }

                        if (h == 0) {
                            lsb3.add(m_gateway.get(i).devices.get(j));
                        }
                    }


                    if ("1".equals(m_gateway.get(i).type)) {
                        lsb9.add(m_gateway.get(i).devices.get(j));
                    }


                } else if ("010F".equals(m_gateway.get(i).devices.get(j).deviceId)) {
                    if (m_gateway.get(i).devices.get(j).deviceName == null) {
                        m_gateway.get(i).devices.get(j).deviceName = "中央空调";
                    }

                    if (lsb7.size() == 0) {
                        lsb7.add(m_gateway.get(i).devices.get(j));
                    } else {
                        int h = 0;
                        for (int g = 0; g < lsb7.size(); g++) {
                            if (m_gateway.get(i).devices.get(j).ieee.equals(lsb7.get(g).ieee) && m_gateway.get(i).devices.get(j).port.equals(lsb7.get(g).port)) {

                                h = 1;

                            }


                        }
                        if (h == 0) {


                            lsb7.add(m_gateway.get(i).devices.get(j));


                        }


                    }


                    if ("1".equals(m_gateway.get(i).type)) {
                        lsb9.add(m_gateway.get(i).devices.get(j));
                    }


                } else if ("0020".equals(m_gateway.get(i).devices.get(j).deviceId)) {
                    if (m_gateway.get(i).devices.get(j).deviceName == null) {
                        m_gateway.get(i).devices.get(j).deviceName = "红外转发器";
                    }
                    if (lsb6.size() == 0) {
                        lsb6.add(m_gateway.get(i).devices.get(j));
                    } else {
                        int h = 0;
                        for (int g = 0; g < lsb6.size(); g++) {
                            if (m_gateway.get(i).devices.get(j).ieee.equals(lsb6.get(g).ieee) && m_gateway.get(i).devices.get(j).port.equals(lsb6.get(g).port)) {

                                h = 1;

                            }


                        }

                        if (h == 0) {


                            lsb6.add(m_gateway.get(i).devices.get(j));


                        }


                    }

                    if ("1".equals(m_gateway.get(i).type)) {
                        lsb9.add(m_gateway.get(i).devices.get(j));
                    }

                } else if ("0009".equals(m_gateway.get(i).devices.get(j).deviceId)) {
                    if (m_gateway.get(i).devices.get(j).deviceName == null) {
                        m_gateway.get(i).devices.get(j).deviceName = "智能插座";
                    }

                    if (lsb8.size() == 0) {
                        lsb8.add(m_gateway.get(i).devices.get(j));
                    } else {
                        int h = 0;
                        for (int g = 0; g < lsb8.size(); g++) {
                            if (m_gateway.get(i).devices.get(j).ieee.equals(lsb8.get(g).ieee) && m_gateway.get(i).devices.get(j).port.equals(lsb8.get(g).port)) {

                                h = 1;

                            }


                        }

                        if (h == 0) {
                            lsb8.add(m_gateway.get(i).devices.get(j));
                        }

                    }
                    if ("1".equals(m_gateway.get(i).type)) {
                        lsb9.add(m_gateway.get(i).devices.get(j));
                    }

                }

            }
        }
        info();
        hand.sendEmptyMessage(10);
    }

    public void info() {
        if (list_sb == null) {
            list_sb = new HashMap<String, ArrayList<SheBeiClass>>();
        } else {
            list_sb.clear();
        }
        if (lsb.size() != 0) {
            String t_ieee0 = lsb.get(0).ieee;
            ArrayList<String> m_ieee = new ArrayList<String>();
            m_ieee.add(t_ieee0);
            for (int i = 0; i < lsb.size(); i++) {
                int x = 0;
                for (int j = 0; j < m_ieee.size(); j++) {
                    if (lsb.get(i).ieee.equals(m_ieee.get(j))) {
                        x = 1;
                    }
                }
                if (x == 0) {
                    m_ieee.add(lsb.get(i).ieee);
                }
            }
            for (int i = 0; i < m_ieee.size(); i++) {
                ArrayList<SheBeiClass> sb = new ArrayList<SheBeiClass>();
                for (int j = 0; j < lsb.size(); j++) {
                    if (lsb.get(j).ieee.equals(m_ieee.get(i))) {
                        sb.add(lsb.get(j));
                    }
                }
                list_sb.put(i + "", sb);
            }
            Log.d("size", list_sb.size() + "");
        }
    }

    public void GetDeviceList() {
        Log.d("是我", "ttttttttttttttt");
        if (username != null) {
            GateWay Gw = new GateWay();
            Gw.userId = userId;
            Gw.status = 1;
            String j = JSON.toJSONString(Gw);
            j = AESUtil.encrypt(j);
            HttpUtils httpUtils = new HttpUtils();
            RequestParams params = new RequestParams();
            params.setHeader("Content-Type", "application/json");
            try {
                params.setBodyEntity(new StringEntity(j, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/device/getDeviceList", params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException arg0, String arg1) {

                            ToastUtils.showToast(getBaseContext(), "网络异常");
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> arg0) {
                            if (arg0.result != null && !("".equals(arg0.result))) {
                                arg0.result = AESUtil.decrypt(arg0.result);
                                Log.d("arg0", arg0.result);
                                JSONObject jb = JSONObject.parseObject(arg0.result);
                                String status = jb.getString("status");
                                if ("0".equals(status)) {
                                    JSONArray ja = jb.getJSONArray("deviceList");
                                    SharedPreferences sp = getSharedPreferences("first", MODE_PRIVATE);
                                    sp.edit().putString("deviceList",ja.toString()).commit();
                                    m_gateway = (ArrayList<GateWay>) JSON.parseArray(ja.toString(), GateWay.class);
                                    Get();
                                }
                            }
                        }

                    });

        }
    }


}



